<?php
namespace app\admin\controller;


use think\Db;
use think\Request;
class Comment extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where['vc_status'] = ['<>',3];
            $where['v_id'] = ['>',0];
            if(isset($keyword) && $keyword != ''){
                $where['vc_content|pp_name|user_username|user_phone|v_title'] = ['like', '%' . $keyword . '%'];
            }
            if(session('agid') == 10){
                $where['pp_id'] = session('uid');
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_video_comment')->alias('c')
                ->join('up_pp_video v','c.vc_v_id = v.v_id','left')
                ->join('up_u_user u','c.vc_user_id = u.user_id','left')
                ->join('up_pp p','v.v_pp_id=p.pp_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_video_comment')->alias('c')
                ->join('up_pp_video v','c.vc_v_id = v.v_id','left')
                ->join('up_u_user u','c.vc_user_id = u.user_id','left')
                ->join('up_pp p','v.v_pp_id=p.pp_id','LEFT')
                ->where($where)
                ->order('vc_time desc')
                ->page($NowPage,$limits)
                ->field('c.*,user_username,user_phone,pp_name,v_title,v_url')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function reply(){
        if(request()->isAjax()){
            $where['vcr_vc_id'] = session('vc_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_video_comment_reply')
                ->join('up_u_user', 'vcr_vc_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_video_comment_reply c')
                ->join('up_u_user u', 'c.vcr_user_id=u.user_id', 'left')
                ->field('c.*,user_username,user_phone')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('vcr_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $vc_id = input('vc_id');
        session('vc_id', $vc_id);
        return $this->fetch();
    }
    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function del(){
        $del_id = input('id');
        try{
            // 删除车辆
            Db::table('up_video_comment')->where('vc_id',$del_id)->delete();
            Db::table('up_video_comment_reply')->where('vcr_v_id',$del_id)->delete();
            // 删除类别
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function comttDel(){
        $del_id = input('id');
        try{
            // 删除车辆
            Db::table('up_video_comment_reply')->where('vcr_id',$del_id)->delete();
            // 删除类别
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

}